import indexDetail from '@/views/system/dept/page/indexDetail.vue'
  import indexEdit from '@/views/system/dept/page/indexEdit.vue'
  export default {
    components: {
      indexDetail,
      indexEdit,
    },
    data() {
      return {
        params: {
          page: 1,
          limit: this.$store.state.ps.PAGE_SIZE,
          parentId: null,
          code: null,
          name: null,
          sort:"asc",
        },
        //头部按钮权限
        headBtn: [],
        //树形加载
        treeloading: true,
        //树形数据
        treeData: [{
          id: 0,
          label: "我是顶层",
          children: []
        }],
        //查询列表
        statusList: [],
        //表格数据
        list: [],
        //表头展示列
        columns: [
          {
            prop: 'name',
            label: '部门名称',
            formatter: (row, column, cellValue) => {
              return this.common.isNull(cellValue)
            },
          },
          {
            prop: 'fdeptName',
            label: '上级部门',
            formatter: (row, column, cellValue) => {
              return this.common.isNull(cellValue)
            },
          },
          {
            prop: 'code',
            label: '部门编号',
            formatter: (row, column, cellValue) => {
              return this.common.isNull(cellValue)
            },
          },
          {
            prop: 'linkMan',
            label: '负责人',
            formatter: (row, column, cellValue) => {
              return this.common.isNull(cellValue)
            },
          },
          {
            prop: 'linkMobile',
            label: '联系电话',
            formatter: (row, column, cellValue) => {
              return this.common.isNull(cellValue)
            },
          },
          {
            prop: 'sort',
            label: '排序',
            formatter: (row, column, cellValue) => {
              return this.common.transformDoubleByTwoAndUp(cellValue)
            },
          },
          {
            prop: 'status',
            label: '状态',
            formatter: (row, column, cellValue) => {
              return this.common.transformDict(cellValue, JSON.parse(this.$store.state.ps.DICT_LIST.statusType))
            },
          },
        ],
        // 按钮展示列
        operates: {
          width: 150,
          fixed: 'right',
          list: []
        },
        //数据总数量
        total: 0,
        //设置分页参数
        pagination: {
          pageIndex: 1,
          pageSize: this.$store.state.ps.PAGE_SIZE,
        },
        //表格基本参数
        options: {
          stripe: false, // 是否为斑马纹 table
          loading: true, // 是否添加表格loading加载动画
          highlightCurrentRow: true, // 是否支持当前行高亮显示
          mutiSelect: false, // 是否支持列表项选中功能
          isFixed: true,
          tableHeight: 340,
        }, // table 的参数
        // 树形默认参数
        defaultProps: {
          children: 'children',
          label: 'label'
        },
      }
    },
    methods: {
      //初始化
      init() {
        let that = this;
        that.options.loading = true;
        that.statusList = JSON.parse(that.$store.state.ps.DICT_LIST.isTrueType);
        that.treeInit();
        that.tableInit();
      },
      //表格初始化
      tableInit() {
        let that = this;
        that.options.loading = true;
        that.$api.systemRequest.deptPage(that.params)
          .then(res => {
            res = res.data
            if (res.code == 0) {
              that.options.loading = false;
              that.list = res.data.pageList;
              that.total = Number(res.data.total)
            } else {
              this.$notify.error({
                title: '错误提示',
                message: res.msg
              });
            }
          })
      },
      //初始化左侧列表
      treeInit() {
        let that = this;
        that.treeloading = true;
        that.$api.systemRequest.deptLeftData(that.params)
          .then(res => {
            res = res.data
            if (res.code == 0) {
              that.treeloading = false;
              that.treeData[0].children = res.data;
              that.$refs.tree.setCurrentKey(0)
            } else {
              this.$notify.error({
                title: '错误提示',
                message: res.msg
              });
            }
          })
      },
      //树形点击
      handleNodeClick(data) {
        let that = this;
        if (data.id == 0) {
          that.params.parentId = null;
          that.init();
        } else {
          that.pagination.pageIndex = 1;
          that.params.page = 1;
          that.params.parentId = data.id;
          that.tableInit();
        }
      },
      //查询
      search() {
        let that = this;
        that.params.page = 1;
        that.pagination.pageIndex = 1;
        that.init();
      },
      //重置查询
      reload() {
        let that = this;
        that.params = {
          page: 1,
          limit: this.$store.state.ps.PAGE_SIZE,
          parentId: null,
          sort:"asc",
        }
        that.pagination.pageIndex = 1;
        that.init();
      },
      //新增
      handleAdd(path) {
        this.$refs.indexEdit.init();
      },
      // 详情
      handleDetail(index, row, path) {
        this.$refs.indexDetail.init(row.deptId);
      },
      // 编辑
      handleEdit(index, row, path) {
        this.$refs.indexEdit.init(row.deptId);
      },
      // 删除
      handleDel(index, row) {
        let that = this;
        that.$confirm('此操作将删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: 'true'
        }).then(() => {
          that.options.loading = true;
          that.$api.systemRequest.deptDelete({
              id: row.deptId
            })
            .then(res => {
              res = res.data
              that.options.loading = false;
              if (res.code == 0) {
                that.search();
                this.$notify.success({
                  title: '成功提示',
                  message: res.msg
                });
              } else {
                this.$notify.error({
                  title: '错误提示',
                  message: res.msg
                });
              }
            })
        }).catch(() => {
          that.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 切换每页显示的数量
      handleSizeChange(pagination) {
        this.pagination = pagination
        this.params.limit = this.pagination.pageSize
        this.init();
      },
      // 切换页码
      handleIndexChange(pagination) {
        this.pagination = pagination
        this.params.page = this.pagination.pageIndex
        this.init();
      },
    },
    mounted() {
      let that = this
      that.init();
      //权限初始化
      var oper = this.powerCommon.powerSet(that.$store.state.ps.PERM_BTN, that);
      that.operates.list = oper.tableOper
      that.headBtn = oper.headerOper
    }
  }